/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.AdminData;
import data.PrestasiData;
import database.PrestasiDB;
import database.IdentitasDB;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Muhammad Dony
 */
public class RekapPrestasiAdminServlet extends HttpServlet {
    HttpSession session;
    private String action;
    PrestasiDB prestasidb;
    PrestasiData listPrestasi;
    
    
        protected void halamanPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        AdminData admin = (AdminData) session.getAttribute("adminMasuk");
        List listidentitas = new IdentitasDB().getIdentitasdata();
        request.setAttribute("listidentitas", listidentitas);  
        List listprestasi = new PrestasiDB().getprestasi();
        request.setAttribute("listprestasi", listprestasi);      
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/view_prestasiList.jsp");
        rd.forward(request, response);
    }
    
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(RekapPrestasiAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(RekapPrestasiAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } else if (action.equals("view")) {
                    try {
                        halamanPrestasiLengkap(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(RekapPrestasiAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(RekapPrestasiAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

           }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        }
          
  }  
    
     
     @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiAdmin dari session
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");
    }
    
     public void halamanPrestasiLengkap(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Rekap Prestasi");
        //request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        List listprestasi = new PrestasiDB().getprestasiById(ID);
        request.setAttribute("listprestasi", listprestasi);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/view_prestasiLengkap.jsp");
        rd.forward(request, response);
    }
}
